import org.apache.http.HttpHost;
import org.jsoup.nodes.Document;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {

    public static List<Map<String, String>> proxydata = new ArrayList<>();
    //需要刷访问量的csdn博客地址
    public static String[] links = {"https://blog.csdn.net/qq_45988496/article/details/115468536",
            "https://blog.csdn.net/qq_45988496/article/details/115414053",
            "https://blog.csdn.net/qq_45988496/article/details/115406318",
            "https://blog.csdn.net/qq_45988496/article/details/115079557",
            "https://blog.csdn.net/qq_45988496/article/details/113727537",
            "https://blog.csdn.net/qq_45988496/article/details/112614856"
    };
    public static int num = 0;


    public static void main(String[] args) throws Exception {

        //使用线程池来操作
        ExecutorService pool = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 3; i++) {
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("线程" + Thread.currentThread().getName() + "启动！");
                    test();


                }
            });
        }
        pool.shutdown();

    }

    public synchronized static void addNum() {
        num += 1;
        try {
            //线程间隔三秒启动，快代理设置了同一时间同一个ip只能访问一次，短时间内次数太多容易被封ip;
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void test() {
        // 第一层循环取出每页的代理ip
        addNum();
        for (int i = num; i < num + 200; i++) {
            //快代理高匿名代理的url规则
            String url = "https://www.kuaidaili.com/free/intr/" + (i + 1) + "/";
            //普通代理url规则
            // String url = "https://www.kuaidaili.com/free/intr/" + (i + 1) + "/";

            //获取代理服务器信息
//            proxydata = getProxy(url);

            Map hashmap = new HashMap();
            hashmap.put("183.247.152.98", "53281");
            hashmap.put("14.20.235.96", "808");
            hashmap.put("190.122.186.222", "8080");
            hashmap.put("14.215.224.254", "3128");
            hashmap.put("60.191.11.249", "3128");
            hashmap.put("119.57.156.90", "53281");
            hashmap.put("61.133.87.228", "55443");
            hashmap.put("124.205.155.152", "9090");
            hashmap.put("183.6.183.35", "3128");
            hashmap.put("119.57.156.90", "53281");
            hashmap.put("183.6.183.35", "3128");
            hashmap.put("121.8.215.106", "9797");
            hashmap.put("36.112.139.146", "3128");
            proxydata.add(hashmap);

            Iterator iter = hashmap.entrySet().iterator();
            // 循环通过代理IP访问link
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                // 生成代理服务器对象
                HttpHost proxy = new HttpHost(entry.getKey().toString(), Integer.parseInt(entry.getValue().toString()),
                        "HTTP");
                int code = 0;
                // 每个link访问一遍
                for (String link : links) {
                    try {
                        code = doget(link, proxy).getCode();
                        // 如果不是200，说明该ip不能访问csdn，直接跳过后续的link，使用下一个代理服务器
                        if (code != 200) {
                            break;
                        }
                        System.out.println("第" + (i + 1) + "页代理地址：" + entry.getKey() + ":" + entry.getValue() + "------访问结果：" + code);
                    } catch (Exception e1) {
                        System.out.println("第" + (i + 1) + "页代理ip无效！" + entry.getKey() + ":" + entry.getValue());
                        // 直接退出循环，无需每个link都去访问
                        break;
                    }
                }
            }
        }
    }

    /**
     * @param url
     * @return
     * @throws Exception
     * @Descript:获取代理服务器信息（ip、端口、类型）
     */
    public static List<Map<String, String>> getProxy(String url) {

        //如果代理服务器网站做了封ip的策略，如果访问过于频繁可能会被封ip，用单线程的话应该不会，但是使用多线程的话很容易被封，我用10个线程跑过，瞬间被封了。
        //可以通过设置代理服务器去访问，去其他代理服务器网站找个免费能用的就行。
        //HttpHost proxy = new HttpHost("163.125.232.238",8118);

        Document doc = null;
        //请求结果
        HttpClientResult result = null;
        try {
            //
            result = doget(url, null);
            //result = doget(url,proxy);

            //如果返回不是200，说明没有获取到代理服务器信息
            if (result.getCode() != 200)
                throw new Exception();
            //通过httpclient取到html网页
            doc = JsoupUtil.getDocByHtml(result.getContent());
        } catch (Exception e) {
            System.out.println("获取代理服务器信息失败！");
        }

        //使用jsoup自带的访问url
        // Document doc = JsoupUtil.getDocByUrl(url);
        List<Map<String, String>> list = JsoupUtil.getData(doc);
        return list;
    }

    //使用post请求
    public static HttpClientResult dopost(String url, HttpHost proxy) throws Exception {
        //设置请求头集合
        Map<String, String> headers = new HashMap<String, String>();
        // headers.put("Cookie", "123");
        headers.put("Connection", "keep-alive");
        headers.put("Accept", "application/json");
        headers.put("Accept-Language", "zh-CN,zh;q=0.9");
        headers.put("User-Agent",
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
        HttpClientResult result = HttpClientUtil.doPost(url, headers, null, proxy);
        return result;
    }

    //使用get请求
    public static HttpClientResult doget(String url, HttpHost proxy) throws Exception {
        //设置请求头集合
        Map<String, String> headers = new HashMap<String, String>();
        // headers.put("Cookie", "123");
        //headers.put("Connection", "keep-alive");
        headers.put("Accept", "application/json");
        headers.put("cache-control", "max-age=0");
        headers.put("authority", "blog.csdn.net");
        headers.put("accept-encoding", "gzip, deflate, br");
        headers.put("Accept-Language", "zh-CN,zh;q=0.9");
        headers.put("User-Agent",
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
        HttpClientResult result = HttpClientUtil.doGet(url, headers, null, proxy);
        return result;
    }
}

